<?php

namespace magein\think\utils\service;


class  Auth
{

    /**
     * @return User
     */
    public static function user(): User
    {
        return new User();
    }

    /**
     * @return bool
     */
    public static function check(): bool
    {
        $authorization = self::authorization();

        if (empty($authorization)) {
            return false;
        }

        // 验证有效期
        if (!self::user()->check($authorization)) {
            return false;
        }

        return true;
    }

    /**
     * @return string
     */
    public static function authorization(): string
    {
        $authorization = '';
        if (isset($_SERVER['Authorization'])) {
            $authorization = $_SERVER['Authorization'];
        } elseif (isset($_SERVER['AUTHORIZATION'])) {
            $authorization = $_SERVER['AUTHORIZATION'];
        } elseif (isset($_SERVER['HTTP_AUTHORIZATION'])) {
            $authorization = $_SERVER['HTTP_AUTHORIZATION'];
        } elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
            $authorization = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
        } elseif (function_exists('apache_request_headers')) {
            $headers = apache_request_headers();
            if (isset($headers['Authorization'])) {
                $authorization = $headers['Authorization'];
            }
        }

        if ($authorization) {
            $authorization = preg_replace('/^Bearer /', '', $authorization);
        }

        return $authorization;
    }
}